﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;
using System.IO;
using BestBuy.Common;
namespace BestBuy.PageUser
{
    public partial class EditProduct : System.Web.UI.Page
    {
        //private Boolean flag1 = false;
        //private Boolean flag2 = false;
        private static String urlstrPhoto = "";
        private static String urlstrBrochoure = "";
        private static String strPhoto = "";
        private static String strBrochoure = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["IDDN"].ToString().Equals(""))
            {
                Response.Redirect("~/Account/Login.aspx");
            }
            if (!Page.IsPostBack)
            {
                if (Request.Params["IdProduct"] != null)
                {
                    LoadDonVi();
                    LoadInfoProduct(int.Parse(Request.Params["IdProduct"].ToString()));
                    //HiddenIDBangTinMua.Value = Request.Params["IDBangTinMua"].ToString();
                }
            }
        }
        private void LoadDonVi()
        {
            cbDonvi1.DataSource = cDonVi.getDonVi();
            cbDonvi1.DataTextField = "TenDonVi";
            cbDonvi1.DataValueField = "IDDonVi";
            cbDonvi1.DataBind();

            cbDonVi2.DataSource = cDonVi.getDonVi();
            cbDonVi2.DataTextField = "TenDonVi";
            cbDonVi2.DataValueField = "IDDonVi";
            cbDonVi2.DataBind();
        }
        private void LoadInfoProduct(int idProduct)
        {
            SanPham sp = cManageProduct.getProduct(idProduct);
            lbDanhMuc.Text = cManageProduct.getCategoryName(sp.IDDanhMuc);
            HiddenCategory.Value = sp.IDDanhMuc.ToString();
            txtProductDetail.Text = sp.ChiTietSanPham;
            txtProductName.Text = sp.TenSanPham;
            txtModel.Text = sp.MaLoaiSanPham;
            txtKeyword.Text = sp.TuKhoa;
            txtDescription.Text = sp.MoTa;
            String urlPhoto = "~/Images/Product/" + sp.HinhAnh;            
            imagePhoto.ImageUrl = urlPhoto;
            txtBrouchoure.Text = sp.TaiLieu;
            StartTimeTextBox.Text = sp.KyHanGiaoHang;
            String[] str = sp.KhaNangCungCap.Split('/');
            EndTimeTextBox.Text = str[0];
            cbDonvi1.SelectedItem.Text = str[1];
            cbDay.Text = str[2];
            txtPlaceOfOrigin.Text = sp.NoiXuatXu;
            String[] str1 = sp.SoLuongDatToiThieu.Split('/');
            txtMinimumOrder.Text = str1[0];
            cbDonVi2.SelectedItem.Text = str1[1];
            String[] str3 = sp.PhuongThucThanhToan.Split('+');
            if (str3[0]=="1")
            {
                CheckBox1.Checked = true;
            }
            if (str3[1]=="1")
            {
                CheckBox2.Checked = true;
            }
            if (str3[2] == "1")
            {
                CheckBox3.Checked = true;
            }
            if (str3[3] == "1")
            {
                CheckBox4.Checked = true;
            }
            strPhoto = sp.HinhAnh;
            strBrochoure = sp.TaiLieu;
            urlstrPhoto = Server.MapPath("~/Images/Product/") + sp.HinhAnh;
            urlstrBrochoure = Server.MapPath("~/Brochoure/") + sp.TaiLieu;
        }        
        protected void btUpdate_Click(object sender, EventArgs e)
        {
            SanPham sp = new SanPham();
            sp.IDSanPham = int.Parse(Request.Params["IdProduct"].ToString());
            sp.ChiTietSanPham = txtProductDetail.Text;
            sp.TenSanPham = txtProductName.Text;
            sp.MaLoaiSanPham = txtModel.Text;
            sp.TuKhoa = txtKeyword.Text;
            sp.MoTa = txtDescription.Text;
            if (uploadPhoto.HasFile)
            {
                if (strPhoto != "")
                {
                    File.Delete(urlstrPhoto);
                }                
                sp.HinhAnh = uploadPhoto.FileName;
                String str = "";
                str = Server.MapPath("~/Images/Product/") + uploadPhoto.FileName;
                uploadPhoto.SaveAs(str);               
            }
            else
            {
                sp.HinhAnh = strPhoto;
            }
            if (upLoadBrochoure.HasFile)
            {
                if (strBrochoure != "")
                {
                    File.Delete(urlstrBrochoure);
                }                
                sp.TaiLieu = upLoadBrochoure.FileName;
                String str = "";
                str = Server.MapPath("~/Brochoure/") + upLoadBrochoure.FileName;
                uploadPhoto.SaveAs(str);
            }            
            else
            {
                sp.TaiLieu = strBrochoure;
            }
            sp.KyHanGiaoHang = StartTimeTextBox.Text;
            sp.KhaNangCungCap = EndTimeTextBox.Text + "/" + cbDonvi1.SelectedItem.Text + "/" + cbDay.Text;
            sp.NoiXuatXu = txtPlaceOfOrigin.Text;
            sp.SoLuongDatToiThieu = txtMinimumOrder.Text + "/" + cbDonVi2.SelectedItem.Text;
            int[] payment = {0,0,0,0};
            if (CheckBox1.Checked)
            {
                payment[0] = 1;
            }
            if (CheckBox2.Checked)
            {
                payment[1] = 1;
            }
            if (CheckBox3.Checked)
            {
                payment[2] = 1;
            }
            if (CheckBox4.Checked)
            {
                payment[3]=1;
            }
            String strpayment = "";
            for (int i=0;i<payment.Count();i++)
            {
                strpayment = strpayment + payment[i].ToString() + "+";
            }
            sp.PhuongThucThanhToan = strpayment;
            sp.IDTaiKhoan = int.Parse(Session["IDDN"].ToString());
            sp.TinhTrang = (int)SanPhamStatus.Active;
            sp.NgayDang = DateTime.Now.Month + "/" + DateTime.Now.Day + "/" + DateTime.Now.Year;
            cManageProduct.updateProduct(sp);
            Response.Redirect("~/PageUser/MbSellling.aspx");
        }
        //protected void btRemovePhoto_Click(object sender, EventArgs e)
        //{
        //    flag1 = true;
        //    imagePhoto.ImageUrl = "";
        //}

        //protected void btRemoveBrochoure_Click(object sender, EventArgs e)
        //{
        //    flag2 = true;
        //    txtBrouchoure.Text = "";
        //}

    }
}